package com.ly.algorithm.leetcode.dp;

/**
 * @Classname Problem583
 * @Description
 *
 * 给定两个单词 word1 和 word2，找到使得 word1 和 word2 相同所需的最小步数，每步可以删除任意一个字符串中的一个字符。
 *
 *  
 *
 * 示例：
 *
 * 输入: "sea", "eat"
 * 输出: 2
 * 解释: 第一步将"sea"变为"ea"，第二步将"eat"变为"ea"
 *
 *
 *
 * @Date 2020/12/24 14:32
 * @Author 冷心影翼
 */
public class Problem583 {

	public static void main(String[] args) {
		Solution583 solution583 = new Solution583(); //18-10
													//etion        etion
		System.out.println(solution583.minDistance("intention", "execution"));

	}
}


class Solution583 {

	public int minDistance(String word1, String word2) {
		int n1 = word1.length();
		int n2 = word2.length();
		int[][] dp = new int[n1+1][n2+1];
		for (int i = 1; i < n1+1; i++) {
			for (int j = 1; j < n2+1; j++) {
				if(word1.charAt(i-1) == word2.charAt(j-1)) {
					dp[i][j] = dp[i-1][j-1]+1;
				}else {
					dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);
				}
			}
		}
		return n1+n2-2*dp[n1][n2];
	}
}
